#include "SunBulletEx.h"
#include "Animation.h"

SunBulletEx::SunBulletEx()
{
	size.x = 288; size.y = 288;
	damage = 30;

	animation_idle = new Animation();
	animation_explode = new Animation();

	animation_idle->Set_Atlas(&atlas_sun_ex);
	animation_idle->Set_interval(50);

	animation_explode->Set_Atlas(&atlas_sun_ex_explode);
	animation_explode->Set_interval(50);
	animation_explode->Set_loop(false);
	animation_explode->Set_callback([&]() {can_remove = true; });

	IMAGE* frame_idle = animation_idle->Get_frame();
	IMAGE* frame_explode = animation_explode->Get_frame();
	explode_render_offset.x = (frame_idle->getwidth() - frame_explode->getwidth()) / 2.f;
	explode_render_offset.y = (frame_idle->getheight() - frame_explode->getheight()) / 2.f;
}

void SunBulletEx::on_collied()
{
	Bullet::on_collied();

	Main_Camera.Shake(20, 350);

	mciSendString(_T("play sun_explode_ex from 0"), NULL, 0, NULL);
}

void SunBulletEx::on_update(int delta)
{
	if (valid)
		position += velocity * (float)delta;

	if (!valid)
		animation_explode->Update(delta);
	else
		animation_idle->Update(delta);

	if (check_if_exceeds_screen())
		can_remove = true;
}

void SunBulletEx::on_draw(const Camera& camera) const
{
	if (valid)
	{
		animation_idle->On_draw(camera, (int)position.x, (int)position.y);
	}
	else
	{
		animation_explode->On_draw(camera,
			(int)(position.x + explode_render_offset.x),
			(int)(position.y + explode_render_offset.y));
	}
	Bullet::on_draw(camera);
}

bool SunBulletEx::check_collied(const Vector2& position, const Vector2& size)
{
	bool is_collide_x = (max(this->position.x + this->size.x, position.x + size.x) -
		min(this->position.x, position.x) <= this->size.x + size.x);
	bool is_collide_y = (max(this->position.y + this->size.y, position.y + size.y) -
		min(this->position.y, position.y) <= this->size.y + size.y);
	return is_collide_x && is_collide_y;
}
